import config from './config.js';
import store from '../store/index.js'
export default {
	request(options = {}) {
		return new Promise((resolve, reject) => {
			// let url = options.url;
			// if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
			// 	options.url = config.domain + url;
			// }
			let token = uni.getStorageSync("token");
			if (token != null && token != "") {
				options.header.token = token;
			}
			options.complete = (response) => {
				if (response.statusCode == 200 || response.statusCode == 0) {
					if (response.data.code == 40004||response.data.code == 40003 || response.data.code == 11012) {
						uni.removeStorageSync("token");
						// uni.removeStorageSync("shopId");
						// uni.removeStorageSync("shopName");
						// uni.removeStorageSync("role");
						// uni.removeStorageSync("menuList");
						store.commit('logout');
						uni.hideLoading();
						uni.showToast({
							title: "身份已过期,请重新登录!",
							icon: "none"
						})
						/*#ifdef MP-WEIXIN*/
						if (store.state.token == "" || store.state.token == null) {
							uni.reLaunch({
								url: "/pages/login/login"
							})
						}
						/*#endif*/
					} else {
						resolve(response.data)
					}
				} else {
					uni.hideLoading();
					uni.showToast({
						title: '请求异常！',
					});
				}
			}
			uni.request(options)
		})
	},
	postToB(url,data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header,
			method: "POST"
		}
		if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
			options.url = config.domainToB + url;
		}
		return this.request(options);
	},
	getToB(url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header
		}
		if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
			options.url = config.domainToB + url;
		}
		return this.request(options);
	},
	putToB(url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header,
			method: "PUT"
		}
		if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
			options.url = config.domainToB + url;
		}
		return this.request(options);
	},
	deleteToB(url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header,
			method: "DELETE",
		}
		if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
			options.url = config.domainToB + url;
		}
		return this.request(options);
	},
	post(url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header,
			method: "POST"
		}
		if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
			options.url = config.domain + url;
		}
		return this.request(options);
	},
	get(url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header
		}
		if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
			options.url = config.domain + url;
		}
		return this.request(options);
	},
	put(url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header,
			method: "PUT"
		}
		if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
			options.url = config.domain + url;
		}
		return this.request(options);
	},
	delete(url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header,
			method: "DELETE"
		}
		if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
			options.url = config.domain + url;
		}
		return this.request(options);
	},
	uploadFile(url, formData) {
		let header = {
			"Content-Type": "application/form-data"
		}
		let options = {
			url: url,
			data: formData,
			header: header,
			method: "DELETE"
		}
		if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
			options.url = config.domain + url;
		}
		return this.request(options);
	}
};